const sequelize = require('../db.js')
const { QueryTypes } = require('sequelize');
const router = require('koa-router')()
const jwt = require('koa-jwt');
router.prefix('/goods')
router.get('/' ,jwt({ secret:'yan' }),async ctx=>{
	console.log(ctx.state,'state')
ctx.body = {
    code:200,
    data:await sequelize.query('select goods2.id,goods2.img,goods2.title,goodsSku.price from goods2,goodsSku where goods2.id=goodsSku.goodsId and goodsSku.price=(select min(price) from goodsSku where goodsId = goods2.id)', { type: QueryTypes.SELECT })
}
})
router.get('/findByCid/:id',async ctx=>{       
    let {id} = ctx.params
    ctx.body = {
        code:200,
        data:await sequelize.query(`select goods2.id,goods2.img,goods2.title,goodsSku.price from goods2,goodsSku where goods2.id=goodsSku.goodsId and goodsSku.price=(select min(price) from goodsSku where goodsId = goods2.id) and goods2.categoryId=${id}`, { type: QueryTypes.SELECT })
    }
})
router.get('/findById/:id',async ctx=>{
    let {id} = ctx.params
    ctx.body = {
        code:200,
        data:await sequelize.query(`select * from goods2,goodsSku where goodsSku.goodsId = ${id} and goods2.id=${id}`, { type: QueryTypes.SELECT })
    }
})

module.exports = router